package org.game.module.datingbo.handler;

import com.sojoys.artifact.tools.ToolError;
import org.cfg.DatingCfg;
import org.cfg.DatingRuleCfg;
import org.constant.DatingConstant;
import org.error.GameErrorCode;
import org.game.constant.DatingTypeConstant;
import org.game.db.bean.*;
import org.game.db.cache.CityDatingRecordCache;
import org.game.log.LogDating;
import org.game.log.builder.LogDsp;
import org.game.log.dsp.DatingDsp;
import org.game.module.datingbo.DatingBO;
import org.game.module.datingbo.DatingType;
import org.game.msgBuilder.DatingMsgBuilder;
import org.game.protobuf.c2s.C2SDatingMsg;
import org.game.protobuf.s2c.S2CShareMsg;
import org.game.util.ProtoUnit;

import java.util.List;
import java.util.Map;

/**
 * Created by fxf on 2018-3-16.
 */
@DatingType(DatingTypeConstant.DATING_TYPE_WORK)
public class WorkDatingHandler extends AbstractDatingHandler {
    @Override
    public byte[] getScript(Player player, C2SDatingMsg.GetScriptMsg msg) {
        CityDatingRecord record = CityDatingRecordCache.me().getById(Long.valueOf(msg.getCityDatingId()));
        ToolError.isAndTrue(GameErrorCode.CLIENT_PARAM_IS_ERR,"没有找到CityDatingRecord，record==null",record==null);
        ToolError.isAndTrue(GameErrorCode.CLIENT_PARAM_IS_ERR,"记录类型错误,record.getDatingType()!=DatingTypeConstant.DATING_TYPE_WORK",record.getDatingType()!=getDatingType());

        DatingRuleCfg datingRuleCfg = record.getCityDatingRecordProxy().getCfg();
        int datingRuleCid = datingRuleCfg.getId();
        ToolError.isAndTrue(GameErrorCode.CFG_IS_ERR, "没有找到据本id，datingRuleCid == 0",datingRuleCid == 0);

        List<Integer> roleCidList = (List<Integer>) datingRuleCfg.getEnterCondition().get(DatingConstant.ENTER_CDTION_KEY_ROLE_CIDS);
        // 筛选分支节点
        Map<Integer, List<Integer>> branchNodes = getScriptBranchNode(player, datingRuleCid,msg);
        // 获取开始节点
        int startNodeId = getStartNodeId(datingRuleCid);
        createCurrentScriptRecord(player, datingRuleCid, branchNodes, startNodeId, roleCidList);

        LogDating.me().log(player.getId(),getDatingType(),datingRuleCid,datingRuleCfg.getId(), LogDsp.newBuilder(DatingDsp.GET_SCRIPT));
        player.getPlayerProxy().sendMsg(DatingBO.sendEveryDayScript,DatingMsgBuilder.getDatingScriptMsg(datingRuleCid,branchNodes));
        return new byte[0];
    }

    @Override
    protected void endDatingRecord(Player player, Dating dating, DatingCfg cfg,int roleCid,CurrentScriptRecord record) {
        super.endDatingRecord(player,dating,cfg,roleCid,record);

        CityDatingRecord cityDatingRecord = CityDatingRecordCache.me().getCityDatingRecordCacheProxy().getByPlayerIdDatingTypeRoleCid(player.getId(),getDatingType(),roleCid);
        //城市约会记录有可能在约会完成之前就被定时器删除掉，删除以后不会影响当前约会
        if (cityDatingRecord != null){
            cityDatingRecord.delete();
            player.getPlayerProxy().sendMsg(DatingBO.sendCityDating,DatingMsgBuilder.getCityDatingInfo(S2CShareMsg.ChangeType.DELETE,cityDatingRecord));
            updateCity(player, cityDatingRecord.getCityDatingRecordProxy().getCfg());
        }
    }
}
